winsafe\oleaut\structs/
bstr.rs1#![allow(non_snake_case)]
2
3use crate::co;
4use crate::decl::*;
5use crate::oleaut::ffi;
6
7#[repr(transparent)]
15pub struct BSTR(*mut u16);
16
17impl Drop for BSTR {
18 fn drop(&mut self) {
19 if !self.0.is_null() {
20 unsafe {
21 ffi::SysFreeString(self.0);
22 }
23 }
24 }
25}
26
27impl Default for BSTR {
28 fn default() -> Self {
29 Self(std::ptr::null_mut())
30 }
31}
32
33impl From<BSTR> for WString {
34 fn from(v: BSTR) -> WString {
35 unsafe { WString::from_wchars_nullt(v.as_ptr()) }
36 }
37}
38
39impl std::fmt::Display for BSTR {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 let txt = unsafe { WString::from_wchars_nullt(self.as_ptr()) };
42 std::fmt::Display::fmt(&txt, f)
43 }
44}
45impl std::fmt::Debug for BSTR {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 write!(f, "BSTR \"{}\"", self)
48 }
49}
50
51impl BSTR {
52 #[must_use]
55 pub fn SysAllocString(s: &str) -> HrResult<Self> {
56 let str_obj = WString::from_str(s);
57 let ptr = unsafe { ffi::SysAllocString(str_obj.as_ptr()) };
58 if ptr.is_null() { Err(co::HRESULT::E_OUTOFMEMORY) } else { Ok(Self(ptr)) }
59 }
60
61 pub fn SysReAllocString(&mut self, s: &str) -> HrResult<()> {
66 let str_obj = WString::from_str(s);
67 let ptr = unsafe { ffi::SysReAllocString(self.0, str_obj.as_ptr()) };
68 if ptr.is_null() {
69 Err(co::HRESULT::E_OUTOFMEMORY)
70 } else {
71 self.0 = ptr;
72 Ok(())
73 }
74 }
75
76 #[must_use]
79 pub fn SysStringLen(&self) -> u32 {
80 unsafe { ffi::SysStringLen(self.0) }
81 }
82
83 #[must_use]
90 pub const unsafe fn from_ptr(p: *mut u16) -> Self {
91 Self(p)
92 }
93
94 #[must_use]
98 pub const fn as_ptr(&self) -> *mut u16 {
99 self.0
100 }
101
102 #[must_use]
106 pub const fn as_mut_ptr(&mut self) -> *mut *mut u16 {
107 &mut self.0
108 }
109
110 #[must_use]
114 pub fn as_slice(&self) -> &[u16] {
115 unsafe { std::slice::from_raw_parts(self.0, self.SysStringLen() as usize + 1) }
116 }
117
118 #[must_use]
127 pub const fn leak(&mut self) -> *mut u16 {
128 std::mem::replace(&mut self.0, std::ptr::null_mut())
129 }
130}